package com.ngocvo.QuanLyThuChi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class NoUpdateActivity extends Activity{
	CashFlow cashFlow = new CashFlow();	
	CashFlowData CFData = new CashFlowData(this);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vayno_layout);
		//Date time formater
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
		//Set flow type
		Global.flowType = FlowType.No;
		//Get the date, time display
		final TextView lbl_title = (TextView)findViewById(R.id.lbl_Thu);
		final TextView txt_DateDisplay = (TextView)findViewById(R.id.txt_NgayDisplay);
		final TextView txt_TimeDisplay = (TextView)findViewById(R.id.txt_CustClock);
		final TextView txt_FlowBackDate = (TextView)findViewById(R.id.txt_NgayTraDisplay);
		final Button btn_Save = (Button)findViewById(R.id.btn_Luu);
		final Button btn_SaveCont = (Button)findViewById(R.id.btn_LuuTiep);
		final Button btn_Update = (Button)findViewById(R.id.btn_Update);
		final EditText edit_Amount = (EditText)findViewById(R.id.edit_SoTien);
		final EditText edit_Name = (EditText)findViewById(R.id.edit_GhiChu);
		
		lbl_title.setText("Cập Nhật Khoản Nợ");
		btn_Save.setVisibility(View.INVISIBLE);
		btn_SaveCont.setVisibility(View.INVISIBLE);
		btn_Update.setVisibility(View.VISIBLE);
		
		Global.updateCurrentDate();
		Calendar c = Calendar.getInstance();		
		Global.strModifiedDate = String.valueOf(c.get(Calendar.YEAR)) + "-" +
								String.valueOf(Global.pad(c.get(Calendar.MONTH)+1)) + "-" +
								String.valueOf(Global.pad(c.get(Calendar.DAY_OF_MONTH))) + " " +
								String.valueOf(Global.pad(c.get(Calendar.HOUR_OF_DAY))) + ":" +
								String.valueOf(Global.pad(c.get(Calendar.MINUTE))) + ":" +
								String.valueOf(Global.pad(c.get(Calendar.SECOND)));
		
		
		Bundle b = this.getIntent().getExtras();
		String cash_id = b.getString("cash_id");			
		
		try{
			cashFlow = CFData.Get(Long.parseLong(cash_id));
			edit_Amount.setText(String.valueOf(cashFlow.getAmount()));
			edit_Name.setText(cashFlow.getName());
			txt_DateDisplay.setText(dateFormat.format(cashFlow.getFlowDate()));
			txt_TimeDisplay.setText(timeFormat.format(cashFlow.getFlowDate()));
			if(cashFlow.getFlowBackDate()==null){
				txt_FlowBackDate.setText(R.string.txt_NgayTraDisplay);
			}else{
				txt_FlowBackDate.setText(dateFormat.format(cashFlow.getFlowBackDate()));
			}
			
			
			
			SimpleDateFormat dbDateFormat = new SimpleDateFormat("yyyy-MM-dd ");
			SimpleDateFormat dbTimeFormat = new SimpleDateFormat("HH:mm:ss");
			Global.strDate = dbDateFormat.format(cashFlow.getFlowDate());
			Global.strTime = dbTimeFormat.format(cashFlow.getFlowDate());
			
			
		}catch (Exception e) {
			AlertDialog.Builder builder = new AlertDialog.Builder(NoUpdateActivity.this);
			builder.setTitle("Error orrcurs!");
			builder.setMessage("The error is: " + e.getMessage());
			builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub								
				}
			});
			builder.show();
		}
		
		//Set event listener
		txt_DateDisplay.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				showDialog(Global.DATE_DIALOG_ID);				
			}
		});
		
		txt_TimeDisplay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.TIME_DIALOG_ID);				
			}
		});
		
		txt_FlowBackDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.RETURN_DATE_DIALOG_ID);					
			}
		});
		
		btn_Update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {				
				Date date;
				try {					
					cashFlow.setAmount(Integer.parseInt(edit_Amount.getText().toString()));
					cashFlow.setName(edit_Name.getText().toString());
					cashFlow.setModifiedDate(Global.dbFormat.parse(Global.strModifiedDate));
					cashFlow.setFlowDate(Global.dbFormat.parse(Global.strDate + Global.strTime));					
					if(txt_FlowBackDate.getText().equals(R.string.txt_NgayTraDisplay)){
						date = null;
						cashFlow.setFlowBackDate(date);
					}else{
						cashFlow.setFlowBackDate(Global.dbFormat.parse(Global.strFlowBackDate + Global.strTime));
					}
				} catch (ParseException e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(NoUpdateActivity.this);
					builder.setTitle("Error orrcurs!");
					builder.setMessage("The error is: " + e.getMessage());
					builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub								
						}
					});
					builder.show();
				}
				try {
					CFData.Update(cashFlow);
					
					Bundle b = new Bundle();
					b.putInt("flowType", FlowType.No.type());
					b.putString("title", getResources().getString(R.string.lbl_thongkeThu));
					
					Intent i = new Intent(NoUpdateActivity.this, ThongKeActivity.class);
					i.putExtras(b);
					startActivity(i);
					
				} catch (Exception e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(NoUpdateActivity.this);
					builder.setTitle("Error orrcurs!");
					builder.setMessage("The error is: " + e.getMessage());
					builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub								
						}
					});
					builder.show();
				}
			}
		});
	}
	
	//Create date and time dialog
	@Override
	protected Dialog onCreateDialog(int id) {
		
		final TextView txt_DateDisplay = (TextView)findViewById(R.id.txt_NgayDisplay);
		final TextView txt_TimeDisplay = (TextView)findViewById(R.id.txt_CustClock);
		final TextView txt_FlowBackDate = (TextView)findViewById(R.id.txt_NgayTraDisplay);
		
		//Create listener when user set date for flow start date
		DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Global.intYear = year;
				Global.intMonth = monthOfYear + 1;
				Global.intDay = dayOfMonth;
				Global.updateDate(txt_DateDisplay);
			}
		};
		
		//Create listener when user set date for flow end date
		DatePickerDialog.OnDateSetListener mReturnDateSetListener = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Global.intYear = year;
				Global.intMonth = monthOfYear + 1;
				Global.intDay = dayOfMonth;
				Global.updateFlowBackDate(txt_FlowBackDate);
			}
		};
		
		//Create listener when user set time for flow start date
		TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				Global.intHour = hourOfDay;
				Global.intMinute = minute;
				Global.updateTime(txt_TimeDisplay);
			}
		};
		switch(id){
			//Create date picker for flow start date
			case Global.DATE_DIALOG_ID:
				return new DatePickerDialog(this, mDateSetListener, Global.intYear, Global.intMonth - 1, Global.intDay);
			//Create date picker for flow end date
			case Global.RETURN_DATE_DIALOG_ID:
				return new DatePickerDialog(this, mReturnDateSetListener, Global.intYear, Global.intMonth - 1, Global.intDay);
			//Create time picker for start date
			case Global.TIME_DIALOG_ID:
				return new TimePickerDialog(this, mTimeSetListener, Global.intHour, Global.intMinute, true);
		}
		return null;
	}
}
